<!-- 设备 -->
<template>
  <div class="equipment">

    <van-nav-bar :title="title" @click-left="() => $router.go(-1)" @click-right="onClickRight" safe-area-inset-top>
      <template #right>
        <span style="color: #000" @click="adddevice()">添加设备</span>
      </template>
      <template #left>
        <img style="width: 25px" src="../../assets/images/back.png" alt="" />
      </template>
    </van-nav-bar>

    <van-tabs v-model="active" color="#3e96e8" background="#F6F6F6" @click="tabclick">
      <van-tab name="1" title="水质仪">
        <first ref="child1"></first>
      </van-tab>
      <van-tab name="2" title="智能开关">
        <second ref="child2"></second>
      </van-tab>
      <van-tab name="3" title="摄像头">
        <third ref="child3"></third>
      </van-tab>
      <!-- <van-tab name="4" title="智能秤">
        <fourth></fourth>
      </van-tab> -->
    </van-tabs>
  </div>
</template>

<script>
import { userDeviceAdd } from "../../api/user";
import first from "./module/first.vue";
import second from "./module/second.vue";
import third from "./module/third.vue";
import fourth from "./module/fourth.vue";
export default {
  name: "",
  components: {
    first,
    second,
    third,
    fourth,
  },
  data() {
    return {
      active: "1",
      title: ''
    };
  },
  methods: {
    // 开关切换
    tabclick(name, title) {
      console.log(title)
      this.title = title;
      var that =this;
      if(title=="水质仪"){
        setTimeout(() => {
          that.$refs.child1.getData();
        }, 200);
      
      }else  if(title=="智能开关"){
        setTimeout(() => {
          that.$refs.child2.getData();
        }, 200);
      
      }else  if(title=="摄像头"){
        setTimeout(() => {
          that.$refs.child3.getData();
        }, 200);
       
      }

      // if (title == "摄像头") {

      //   let list = await deviceList({ userid: localStorage.getItem("userid") });

      // }

    },
    // 添加设备
    onClickRight() { },
    adddevice(){
      console.log("ss");
      try{
        window.ReactNativeWebView.qrCode()
      }catch(e){

      }

     // qrResult('{"type":"2","key":"sky","code":"D0697125"}');
    // qrResult('{"type":"3","key":"sky","code":"K07965317"}');
    }
  },
  computed: {},
  watch: {},
  created() { },
  mounted() {
    let arr = ['水质仪', '智能开关', '摄像头'];
    this.title = this.$route.query.type;

    console.log("this.$route.query.type", this.$route.query.type);

    if (this.$route.query.type == "水质仪") {
      this.active = "1"
    } else if (this.$route.query.type == "智能开关") {
      this.active = "2"
    } else if (this.$route.query.type == "摄像头") {
      this.active = "3"
    }
    var aid = this.$route.query.aid;
    var that =this;
    window.qrResult = async function (data) {

 
          var json = JSON.parse(data);
          if (json.key == "sky") {
            var code = json.code;
            var type = json.type;
            var userid = localStorage.getItem("userid");
            var areaid = aid;
            let res= await userDeviceAdd({ code: code, type: type, userid: userid, areaid: areaid });
            if(res.status==200){
              that.$toast("添加成功");
              if(that.active=="1"){
                that.$refs.child1.getData();
              }else  if(that.active=="2"){
                that.$refs.child2.getData();
              }else  if(that.active=="3"){
                that.$refs.child3.getData();
              }
            
            }
          }

      }



  },
};
</script>

<style lang="scss" scoped>
.equipment {
  width: 100%;
}
</style>
